﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DealKhuyenMaiV2.com.Models;
using PagedList;

namespace DealKhuyenMaiV2.com.Controllers
{
    public class MemberManagerController : BaseController
    {
        // GET: /CompanyManager/

        public ViewResult Index()
        {
            var accounts = DbConText.Accounts.Include("TypeAccount").Include("Ward").Where(a => a.IDTypeAccount == 1 && a.Active == true);
            int pageSize = 10;
            ViewBag.Methol = "Index";
            return View(accounts.ToList().ToPagedList(1, pageSize));
        }
        [HttpPost]
        public PartialViewResult Index(int? page)
        {
            var accounts = DbConText.Accounts.Include("TypeAccount").Include("Ward").Where(a => a.IDTypeAccount == 1 && a.Active == true);
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            ViewBag.Methol = "Index";
            return PartialView("_ListMember", accounts.ToList().ToPagedList(pageNumber, pageSize));
        }
        [HttpPost]
        public PartialViewResult Search(int? page, string KeyWord, string Display)
        {
            var accounts = DbConText.Accounts.Include("TypeAccount").Include("Ward").Where
                (
                a => ((a.Email.IndexOf(KeyWord)) > -1 || KeyWord.IndexOf((string)a.Email) > -1
                    || a.FullName.IndexOf(KeyWord) > -1 || KeyWord.IndexOf((string)a.FullName) > -1)
                   
                )
                .Where(a => a.IDTypeAccount == 1 && a.Active == true);
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            ViewBag.Methol = "Search";
            ViewBag.Display = Display;
            ViewBag.KeyWord = KeyWord;
            return PartialView("_ListMember", accounts.ToList().ToPagedList(pageNumber, pageSize));
        }

        //
        // GET: /CompanyManager/Details/5

        public ViewResult Details(int id)
        {
            Account account = DbConText.Accounts.Single(a => a.IDAccount == id);
            return View(account);
        }

        //
        // GET: /CompanyManager/Create

        public ActionResult Create()
        {
            ViewBag.IDTypeAccount = new SelectList(DbConText.TypeAccounts, "IDTypeAccount", "NameTypeAccount");
            ViewBag.IDWard = new SelectList(DbConText.Wards, "IDWard", "NameWard");
            return View();
        }

        //
        // POST: /CompanyManager/Create

        [HttpPost]
        public ActionResult Create(Account account)
        {
            if (ModelState.IsValid)
            {
                DbConText.Accounts.AddObject(account);
                DbConText.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.IDTypeAccount = new SelectList(DbConText.TypeAccounts, "IDTypeAccount", "NameTypeAccount", account.IDTypeAccount);
            ViewBag.IDWard = new SelectList(DbConText.Wards, "IDWard", "NameWard", account.IDWard);
            return View(account);
        }

        //
        // GET: /CompanyManager/Edit/5

        public ActionResult Edit(int id)
        {
            Account account = DbConText.Accounts.Single(a => a.IDAccount == id && a.Active == true);
            ViewBag.IDTypeAccount = new SelectList(DbConText.TypeAccounts, "IDTypeAccount", "NameTypeAccount", account.IDTypeAccount);
            ViewBag.IDWard = new SelectList(DbConText.Wards, "IDWard", "NameWard", account.IDWard);
            return View(account);
        }

        //
        // POST: /CompanyManager/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Account account)
        {
            Account _account = DbConText.Accounts.Single(a => a.IDAccount == id );
            {
                _account.Email = account.Email;
                //_account.Password = account.Password;
                //_account.IDTypeAccount = account.IDTypeAccount;
                _account.AddressReceive = account.AddressReceive;
                //_account.IDWard = account.IDWard;
                //_account.DateRegister = account.DateRegister;
                _account.AutoReceiveEmail = account.AutoReceiveEmail;
                _account.PhoneNumber = account.PhoneNumber;
                _account.FullName = account.FullName;
                _account.Description = account.Description;
                _account.Longitude = account.Longitude;
                _account.Latitude = account.Latitude;
                //_account.Image = account.Image;
                _account.Active = account.Active;
            }
            if (ModelState.IsValid)
            {
                DbConText.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                //for debug only
                var errors = ModelState
    .Where(x => x.Value.Errors.Count > 0)
    .Select(x => new { x.Key, x.Value.Errors })
    .ToArray();
            }


            ViewBag.IDTypeAccount = new SelectList(DbConText.TypeAccounts, "IDTypeAccount", "NameTypeAccount", account.IDTypeAccount);
            ViewBag.IDWard = new SelectList(DbConText.Wards, "IDWard", "NameWard", account.IDWard);
            return View(account);
        }

        //
        // GET: /CompanyManager/Delete/5

        [HttpPost, ActionName("Delete")]
        public PartialViewResult DeleteConfirmed(int? page, int id)
        {
            Account account = DbConText.Accounts.Single(a => a.IDAccount == id);
            account.Active = false;
            DbConText.SaveChanges();

            var accounts = DbConText.Accounts.Include("TypeAccount").Include("Ward").Where(a => a.IDTypeAccount == 1 && a.Active == true);
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            ViewBag.Methol = "Index";
            return PartialView("_ListMember", accounts.ToList().ToPagedList(pageNumber, pageSize));
        }
    }
}
